package com.lantosec.hmrp.modules.revisit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.shiro.TokenUtil;
import com.lantosec.hmrp.modules.revisit.entity.HisPatientInfo;
import com.lantosec.hmrp.modules.revisit.entity.RevisitConfig;
import com.lantosec.hmrp.modules.revisit.entity.RevisitTask;
import com.lantosec.hmrp.modules.revisit.mapper.RevisitTaskMapper;
import com.lantosec.hmrp.modules.revisit.service.IRevisitTaskService;
import com.lantosec.hmrp.modules.revisit.vo.RevisitTaskVO;
import com.lantosec.hmrp.modules.system.entity.SysDeptUser;
import com.lantosec.hmrp.modules.system.service.ISysDeptService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 回访任务 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-04-28
 */
@Service
@Primary
@Log4j2
@SuppressWarnings("all")
public class RevisitTaskServiceImpl extends ServiceImpl<RevisitTaskMapper, RevisitTask> implements IRevisitTaskService {

    @Autowired
    private RevisitConfigServiceImpl revisitConfigService;

    @Autowired
    private ISysDeptService iSysDeptService;

    @Override
    public List<RevisitTaskVO> getNoReturnVisit(PageVO<RevisitTaskVO> page, Integer permissionsType) {
        if (permissionsType == null) {
            permissionsType = 1;
        }
        //获取当前用户id和名称
        String userId = TokenUtil.getUserId();
        String deptIds = TokenUtil.getDeptId();
        // 查询该用户的岗位
        String position = baseMapper.getPositionByUserId(userId,deptIds);
        if (position.equals("医生")) {
            //如果当前用户是主管医生 只查询user_id为当前用户的未回访的列表
            Integer doctorState = 0;
            List<RevisitTaskVO> revisitTaskVOList = getRevisitTask(page, permissionsType, userId, doctorState);
            revisitTaskVOList.forEach(parm -> {
                String deptId = parm.getDeptId();
                //查询当前部门的回访功能配置
                RevisitConfig revisitConfig = revisitConfigService.getOne(new QueryWrapper<RevisitConfig>().eq(RevisitConfig.ColName.dept_id.toString(), deptId));
                //如果配置表中的医生回访为必回访 主管医生看到的状态是doctorState
                if (revisitConfig.getRevisitDoctor() == 1) {
                    Integer doctorState1 = parm.getDoctorState();
                    parm.setState(doctorState1);
                } else {
                    parm.setState(0);
                }
                //查询回访任务中的患者信息
                String patientId = parm.getPatientId();
                HisPatientInfo hisPatientInfo = baseMapper.getHisPatientInfo(patientId);
                parm.setHisPatientInfo(hisPatientInfo);
            });
            page.setRecords(revisitTaskVOList);
            return revisitTaskVOList;
        }
        if (position.equals("护士")) {
            //如果当前用户是护士 只查询当前用户所属部门=dept_id的未回访的列表
            Integer nurseState = 0;
            List<RevisitTaskVO> revisitTaskVOList1 = baseMapper.getNoReturnVisit1(page, nurseState, deptIds);
            revisitTaskVOList1.forEach(parm1 -> {
                String deptId = parm1.getDeptId();
                //查询当前部门的回访功能配置
                RevisitConfig revisitConfig = revisitConfigService.getOne(new QueryWrapper<RevisitConfig>().eq(RevisitConfig.ColName.dept_id.toString(), deptId));
                //如果配置表中的医生回访为必回访且护士回访为非必回访
                if (revisitConfig.getRevisitDoctor() == 1 && revisitConfig.getRevisitNurse() == 0) {
                    Integer doctorState = parm1.getDoctorState();
                    //如果医生回访记录为空,护士看到的状态为当前任务中nurse_state值
                    if (doctorState == 0) {
                        parm1.setState(parm1.getNurseState());
                    } else {
                        //否则为doctor_state值
                        parm1.setState(parm1.getDoctorState());
                    }
                }
                //如果配置表中的医生回访为必回访且护士回访为必回访,返回状态为nurse_state值
                if (revisitConfig.getRevisitDoctor() == 1 && revisitConfig.getRevisitNurse() == 1) {
                    Integer nurseState1 = parm1.getNurseState();
                    parm1.setState(nurseState1);
                }
            });
            page.setRecords(revisitTaskVOList1);
            return revisitTaskVOList1;
        } else {
            //如果当前用户即不是主管医生也不是护士，查询所有未回访的列表
            Integer doctorState = 0;
            Integer nurseState = 0;
            List<RevisitTaskVO> revisitTaskVOList2 = baseMapper.getNoReturnVisit2(page, doctorState, nurseState);
            revisitTaskVOList2.forEach(parm2 -> {
                //查询每个医生下的患者信息
                HisPatientInfo hisPatientInfos = baseMapper.getHisPatientInfo(parm2.getPatientId());
                parm2.setHisPatientInfo(hisPatientInfos);
                String deptId = parm2.getDeptId();
                //查询当前部门的回访功能配置
                RevisitConfig revisitConfig = revisitConfigService.getOne(new QueryWrapper<RevisitConfig>().eq(RevisitConfig.ColName.dept_id.toString(), deptId));
                //如果配置表中的医生回访为必回访且护士回访为非必回访
                if (revisitConfig.getRevisitDoctor() == 1 && revisitConfig.getRevisitNurse() == 0) {
                    parm2.setState(parm2.getDoctorState());
                }
                //如果配置表中的医生回访为必回访且护士回访为必回访
                if (revisitConfig.getRevisitDoctor() == 1 && revisitConfig.getRevisitNurse() == 1) {
                    Integer nurseState1 = parm2.getNurseState();
                    Integer doctorState1 = parm2.getDoctorState();
                    if (nurseState1 == 2 || doctorState1 == 2) {
                        parm2.setState(2);
                    }
                    if (nurseState1 == 1 && doctorState1 == 1) {
                        parm2.setState(1);
                    } else {
                        parm2.setState(0);
                    }
                }
            });
            page.setRecords(revisitTaskVOList2);
            return revisitTaskVOList2;
        }
    }


    /**
     * @date: 2020-06-30 22:18
     * @instructions: 增加权限 ,默认个人权限，当登录用户部门id等于患者出院科室id，为科室权限展示数据
     * @param: permissionsType 1个人2科室3全院
     * @return:
     */
    private List<RevisitTaskVO> getRevisitTask(PageVO<RevisitTaskVO> page, Integer permissionsType, String userId, Integer doctorState) {
        String deptId = TokenUtil.getDeptId();
        List<RevisitTaskVO> list = new ArrayList<>();
        List<RevisitTaskVO> noReturnVisit = baseMapper.getNoReturnVisit(page, doctorState, userId, permissionsType);
        if (permissionsType == 2) {
            noReturnVisit.forEach(it -> {
                if (deptId.equals(it.getDeptId())) {
                    list.add(it);
                }
            });
            return list;
        }
        return noReturnVisit;
    }

    @Override
    public List<RevisitTaskVO> getReturnVisit(PageVO<RevisitTaskVO> page, Integer permissionsType) {
        if (null == permissionsType) {
            permissionsType = 1;
        }
        //获取当前用户id和名称
        String userId = TokenUtil.getUserId();
        String deptIds = TokenUtil.getDeptId();
        // 查询该用户的岗位
        String position = baseMapper.getPositionByUserId(userId,deptIds);
        if (position.equals("医生")) {
            //如果当前用户是主管医生 只查询user_id为当前用户的已回访的列表
            Integer doctorState = 1;
            List<RevisitTaskVO> revisitTaskVOList = getRevisitTask(page, permissionsType, userId, doctorState);
            revisitTaskVOList.forEach(parm -> {
                String deptId = parm.getDeptId();
                //查询当前部门的回访功能配置
                RevisitConfig revisitConfig = revisitConfigService.getOne(new QueryWrapper<RevisitConfig>().eq(RevisitConfig.ColName.dept_id.toString(), deptId));
                //如果配置表中的医生回访为必回访 主管医生看到的状态是doctorState
                if (revisitConfig.getRevisitDoctor() == 1) {
                    Integer doctorState1 = parm.getDoctorState();
                    parm.setState(doctorState1);
                } else {
                    parm.setState(0);
                }
                //查询回访任务中的患者信息
                String patientId = parm.getPatientId();
                HisPatientInfo hisPatientInfo = baseMapper.getHisPatientInfo(patientId);
                parm.setHisPatientInfo(hisPatientInfo);
            });
            page.setRecords(revisitTaskVOList);
            return revisitTaskVOList;
        }
        if (position.equals("护士")) {
            //如果当前用户是护士 只查询当前用户所属部门=dept_id的已回访的列表
            Integer nurseState = 1;
            List<RevisitTaskVO> revisitTaskVOList1 = baseMapper.getNoReturnVisit1(page, nurseState, deptIds);
            revisitTaskVOList1.forEach(parm1 -> {
                String deptId = parm1.getDeptId();
                //查询当前部门的回访功能配置
                RevisitConfig revisitConfig = revisitConfigService.getOne(new QueryWrapper<RevisitConfig>().eq(RevisitConfig.ColName.dept_id.toString(), deptId));
                //如果配置表中的医生回访为必回访且护士回访为非必回访
                if (revisitConfig.getRevisitDoctor() == 1 && revisitConfig.getRevisitNurse() == 0) {
                    Integer doctorState = parm1.getDoctorState();
                    //如果医生回访记录为空,护士看到的状态为当前任务中nurse_state值
                    if (doctorState == 0) {
                        parm1.setState(parm1.getNurseState());
                    } else {
                        //否则为doctor_state值
                        parm1.setState(parm1.getDoctorState());
                    }
                }
                //如果配置表中的医生回访为必回访且护士回访为必回访,返回状态为nurse_state值
                if (revisitConfig.getRevisitDoctor() == 1 && revisitConfig.getRevisitNurse() == 1) {
                    Integer nurseState1 = parm1.getNurseState();
                    parm1.setState(nurseState1);
                }
            });
            page.setRecords(revisitTaskVOList1);
            return revisitTaskVOList1;
        } else {
            //如果当前用户即不是主管医生也不是护士，查询所有已回访的列表
            Integer doctorState = 1;
            Integer nurseState = 1;
            List<RevisitTaskVO> revisitTaskVOList2 = baseMapper.getNoReturnVisit2(page, doctorState, nurseState);
            revisitTaskVOList2.forEach(parm2 -> {
                //查询每个医生下的患者信息
                HisPatientInfo hisPatientInfos = baseMapper.getHisPatientInfo(parm2.getPatientId());
                parm2.setHisPatientInfo(hisPatientInfos);
                String deptId = parm2.getDeptId();
                //查询当前部门的回访功能配置
                RevisitConfig revisitConfig = revisitConfigService.getOne(new QueryWrapper<RevisitConfig>().eq(RevisitConfig.ColName.dept_id.toString(), deptId));
                //如果配置表中的医生回访为必回访且护士回访为非必回访
                if (revisitConfig.getRevisitDoctor() == 1 && revisitConfig.getRevisitNurse() == 0) {
                    parm2.setState(parm2.getDoctorState());
                }
                //如果配置表中的医生回访为必回访且护士回访为必回访
                if (revisitConfig.getRevisitDoctor() == 1 && revisitConfig.getRevisitNurse() == 1) {
                    Integer nurseState1 = parm2.getNurseState();
                    Integer doctorState1 = parm2.getDoctorState();
                    if (nurseState1 == 2 || doctorState1 == 2) {
                        parm2.setState(2);
                    }
                    if (nurseState1 == 1 && doctorState1 == 1) {
                        parm2.setState(1);
                    } else {
                        parm2.setState(0);
                    }
                }
            });
            page.setRecords(revisitTaskVOList2);
            return revisitTaskVOList2;
        }
    }

    @Override
    public List<RevisitTaskVO> getOvertimeReturnVisit(PageVO<RevisitTaskVO> page, Integer permissionsType) {
        if (null == permissionsType) {
            permissionsType = 1;
        }
        //获取当前用户id和名称
        String userId = TokenUtil.getUserId();
        String deptIds = TokenUtil.getDeptId();
        // 查询该用户的岗位
        String position = baseMapper.getPositionByUserId(userId,deptIds);
        if (position.equals("医生")) {
            //如果当前用户是主管医生 只查询user_id为当前用户的超时回访的列表
            Integer doctorState = 2;
            List<RevisitTaskVO> revisitTaskVOList = getRevisitTask(page, permissionsType, userId, doctorState);
            revisitTaskVOList.forEach(parm -> {
                String deptId = parm.getDeptId();
                //查询当前部门的回访功能配置
                RevisitConfig revisitConfig = revisitConfigService.getOne(new QueryWrapper<RevisitConfig>().eq(RevisitConfig.ColName.dept_id.toString(), deptId));
                //如果配置表中的医生回访为必回访 主管医生看到的状态是doctorState
                if (revisitConfig.getRevisitDoctor() == 1) {
                    Integer doctorState1 = parm.getDoctorState();
                    parm.setState(doctorState1);
                } else {
                    parm.setState(0);
                }
                //查询回访任务中的患者信息
                String patientId = parm.getPatientId();
                HisPatientInfo hisPatientInfo = baseMapper.getHisPatientInfo(patientId);
                parm.setHisPatientInfo(hisPatientInfo);
            });
            page.setRecords(revisitTaskVOList);
            return revisitTaskVOList;
        }
        if (position.equals("护士")) {
            //如果当前用户是护士 只查询当前用户所属部门=dept_id的超时回访的列表
            Integer nurseState = 2;
            List<RevisitTaskVO> revisitTaskVOList1 = baseMapper.getNoReturnVisit1(page, nurseState, deptIds);
            revisitTaskVOList1.forEach(parm1 -> {
                String deptId = parm1.getDeptId();
                //查询当前部门的回访功能配置
                RevisitConfig revisitConfig = revisitConfigService.getOne(new QueryWrapper<RevisitConfig>().eq(RevisitConfig.ColName.dept_id.toString(), deptId));
                //如果配置表中的医生回访为必回访且护士回访为非必回访
                if (revisitConfig.getRevisitDoctor() == 1 && revisitConfig.getRevisitNurse() == 0) {
                    Integer doctorState = parm1.getDoctorState();
                    //如果医生回访记录为空,护士看到的状态为当前任务中nurse_state值
                    if (doctorState == 0) {
                        parm1.setState(parm1.getNurseState());
                    } else {
                        //否则为doctor_state值
                        parm1.setState(parm1.getDoctorState());
                    }
                }
                //如果配置表中的医生回访为必回访且护士回访为必回访,返回状态为nurse_state值
                if (revisitConfig.getRevisitDoctor() == 1 && revisitConfig.getRevisitNurse() == 1) {
                    Integer nurseState1 = parm1.getNurseState();
                    parm1.setState(nurseState1);
                }
            });
            page.setRecords(revisitTaskVOList1);
            return revisitTaskVOList1;
        } else {
            //如果当前用户即不是主管医生也不是护士，查询所有超时回访的列表
            Integer doctorState = 2;
            Integer nurseState = 2;
            List<RevisitTaskVO> revisitTaskVOList2 = baseMapper.getNoReturnVisit2(page, doctorState, nurseState);
            revisitTaskVOList2.forEach(parm2 -> {
                //查询每个医生下的患者信息
                HisPatientInfo hisPatientInfos = baseMapper.getHisPatientInfo(parm2.getPatientId());
                parm2.setHisPatientInfo(hisPatientInfos);
                String deptId = parm2.getDeptId();
                //查询当前部门的回访功能配置
                RevisitConfig revisitConfig = revisitConfigService.getOne(new QueryWrapper<RevisitConfig>().eq(RevisitConfig.ColName.dept_id.toString(), deptId));
                //如果配置表中的医生回访为必回访且护士回访为非必回访
                if (revisitConfig.getRevisitDoctor() == 1 && revisitConfig.getRevisitNurse() == 0) {
                    parm2.setState(parm2.getDoctorState());
                }
                //如果配置表中的医生回访为必回访且护士回访为必回访
                if (revisitConfig.getRevisitDoctor() == 1 && revisitConfig.getRevisitNurse() == 1) {
                    Integer nurseState1 = parm2.getNurseState();
                    Integer doctorState1 = parm2.getDoctorState();
                    if (nurseState1 == 2 || doctorState1 == 2) {
                        parm2.setState(2);
                    }
                    if (nurseState1 == 1 && doctorState1 == 1) {
                        parm2.setState(1);
                    } else {
                        parm2.setState(0);
                    }
                }
            });
            page.setRecords(revisitTaskVOList2);
            return revisitTaskVOList2;
        }
    }
}
